package dongze.com.ckdj;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import javax.inject.Inject;

import dongze.com.ckdj.activity.BaseActivity;
import dongze.com.ckdj.data.DataLayer;
import dongze.com.ckdj.map.MapUtil;
import dongze.com.ckdj.persistence.dao.Image;
import dongze.com.ckdj.service.IImageUploadCallback;
import dongze.com.ckdj.service.IImageUploadService;
import dongze.com.ckdj.service.ImageUploadService;
import dongze.com.ckdj.service.ImageUploadTask;
import dongze.com.ckdj.service.uploader.MassLossUploader;
import io.reactivex.Observer;
import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

/**
 * Created by fanqwz on 2017/8/21.
 */

public class TestServiceActivity extends BaseActivity{
    private static final String TAG = TestServiceActivity.class.getSimpleName();
    IImageUploadService service;
    ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder s) {
            TestServiceActivity.this.service = IImageUploadService.Stub.asInterface(s);
            try {
                TestServiceActivity.this.service.addCallback(callback);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            service = null;
        }
    };

    IImageUploadCallback callback = new IImageUploadCallback.Stub() {


        @Override
        public void onTaskProgressChanged(ImageUploadTask task, int progress) throws RemoteException {

        }

        @Override
        public void onTaskFinished(ImageUploadTask task) throws RemoteException {

        }

        @Override
        public void onTaskFailed(ImageUploadTask task) throws RemoteException {

        }

        @Override
        public void onTaskAdded(ImageUploadTask task) throws RemoteException {

        }

        @Override
        public void onTaskDeleted(ImageUploadTask task) throws RemoteException {

        }

        @Override
        public void onTaskCanceled(ImageUploadTask task) throws RemoteException {

        }

        @Override
        public void onNetStateChanged(int state) throws RemoteException {

        }
    };

    @Inject
    Scheduler scheduler;
    @Inject
    DataLayer dataLayer;
    MapUtil mapUtil;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bindService(new Intent(getApplicationContext(), ImageUploadService.class), connection, BIND_AUTO_CREATE);

//        getAppCompenent().inject(this);
//        mapUtil = new MapUtil(getAppCompenent());
//
//
//        ImageUploadTask t = new ImageUploadTask();
//        t.setImagePath("/sdcard/DZDJ/image/2017-08-18-04-40-26.jpg");
//        MassLossUploader massLossUploader = new MassLossUploader(scheduler, AndroidSchedulers.mainThread(), t,"","", new Image(), mapUtil, dataLayer);
//
//        massLossUploader.observe(new Observer<Object>() {
//            @Override
//            public void onSubscribe(Disposable d) {
//                MyLog.d(TAG, "onSubscribe: ");
//            }
//
//            @Override
//            public void onNext(Object value) {
//                MyLog.d(TAG, "onNext: " + value);
//            }
//
//            @Override
//            public void onError(Throwable e) {
//                MyLog.d(TAG, "onError: " + e);
//            }
//
//            @Override
//            public void onComplete() {
//                MyLog.d(TAG, "onComplete: ");
//            }
//        });
//        massLossUploader.start();
    }

    @Override
    protected void onDestroy() {
        unbindService(connection);
        super.onDestroy();
    }
}
